package _binary_search;

import java.util.Arrays;

/**
 * 1283. 使结果不超过阈值的最小除数
 */
public class No1283 {
    public int smallestDivisor(int[] nums, int threshold) {
        int n = nums.length;
        int ans = Integer.MAX_VALUE;
        Arrays.sort(nums);

        int left = 1, right = nums[n - 1];
        while (left <= right) {
            int mid = left + (right - left >> 1);
            int curSum = 0;
            for (int num : nums) {
                curSum += (int) Math.ceil((double) num / mid);
            }
            if (curSum > threshold) left = mid + 1;
            else {
                right = mid - 1;
                ans = Math.min(mid, ans);
            }
        }
        return ans;
    }
}
